package com.example.huichaoliu;
import java.util.ArrayList;

import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class HomeActivity extends FragmentActivity implements OnClickListener,OnPageChangeListener{

	private ViewPager viewPager = null;
	private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
	private ImageButton ic_left = null;
	private ImageButton ic_right = null;
	private ImageView  ic_mall = null;
	private ImageView  ic_fitting = null;
	private ImageView  ic_cool = null;
	private ImageView  ic_mine = null;
	private Fragment mallFragment = null;
	private Fragment coolFragment = null;
	private Fragment fittingFragment = null;
	private Fragment mineFragment = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
	        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
	        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 
		}
        
        init();
	}
	
	private void init() {
		this.viewPager = (ViewPager) findViewById(R.id.viewPager);

		ic_left = (ImageButton) findViewById(R.id.left_button);
		ic_right = (ImageButton) findViewById(R.id.right_img_button);
		ic_left.setVisibility(View.GONE);
		ic_right.setVisibility(View.GONE);
		mallFragment = new MallFragment();
		coolFragment = new CoolFragment();
		fittingFragment = new FittingFragment();
		mineFragment = new MineFragment();
		fragmentList.add(mallFragment);
		fragmentList.add(fittingFragment);
		fragmentList.add(coolFragment);
		fragmentList.add(mineFragment);
		viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
		viewPager.setOnPageChangeListener(this);
		findViewById(R.id.layout_mall).setOnClickListener(this);
		findViewById(R.id.layout_fitting).setOnClickListener(this);
		findViewById(R.id.layout_cool).setOnClickListener(this);
		findViewById(R.id.layout_mine).setOnClickListener(this);
		ic_left.setOnClickListener(this);
		findViewById(R.id.right_img_button).setOnClickListener(this);
		
		ic_mall = (ImageView) findViewById(R.id.ic_mall);
		ic_fitting = (ImageView) findViewById(R.id.ic_fitting);
		ic_cool = (ImageView) findViewById(R.id.ic_cool);
		ic_mine = (ImageView) findViewById(R.id.ic_mine);
	}
	
	
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.layout_mall:
			ic_mall.setImageResource(R.drawable.ic_mall_selected);
			ic_fitting.setImageResource(R.drawable.ic_fitting_normal);
			ic_cool.setImageResource(R.drawable.ic_cool_normal);
			ic_mine.setImageResource(R.drawable.ic_mine_normal);
			viewPager.setCurrentItem(0);
			break;
		case R.id.layout_fitting:
			ic_mall.setImageResource(R.drawable.ic_mall_normal);
			ic_fitting.setImageResource(R.drawable.ic_fitting_selected);
			ic_cool.setImageResource(R.drawable.ic_cool_normal);
			ic_mine.setImageResource(R.drawable.ic_mine_normal);
			viewPager.setCurrentItem(1);
			break;
		case R.id.layout_cool:
			ic_mall.setImageResource(R.drawable.ic_mall_normal);
			ic_fitting.setImageResource(R.drawable.ic_fitting_normal);
			ic_cool.setImageResource(R.drawable.ic_cool_selected);
			ic_mine.setImageResource(R.drawable.ic_mine_normal);
			viewPager.setCurrentItem(2);
			break;
		case R.id.layout_mine:
			ic_mall.setImageResource(R.drawable.ic_mall_normal);
			ic_fitting.setImageResource(R.drawable.ic_fitting_normal);
			ic_cool.setImageResource(R.drawable.ic_cool_normal);
			ic_mine.setImageResource(R.drawable.ic_mine_selected);
			viewPager.setCurrentItem(3);
			break;
		case R.id.left_button:
			if(viewPager.getCurrentItem() == 1){
				if(!Config.isOnline(this)){
					return;
				}
				startActivity(new Intent(this,BagActivity.class));
			}else{
			}
			break;
		case R.id.right_img_button:
			if(viewPager.getCurrentItem() == 1){
				((FittingFragment) fittingFragment).showTypeSelected();
			}else{
				if(!Config.isOnline(this)){
					return;
				}
				if(((CoolFragment) coolFragment).getType() == CoolFragment.TYPE_HELP){
					startActivity(new Intent(this,CallHelpActivity.class));
				}else if(((CoolFragment) coolFragment).getType() == CoolFragment.TYPE_PHOTO){
					startActivity(new Intent(this,CameraFeedActivity.class));
				}
			}
			break;
		}
	}


	public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
	    private ArrayList<Fragment> fragmentsList = new ArrayList<Fragment>();
	 
	    public MyFragmentPagerAdapter(FragmentManager fm) {
	        super(fm);
	    }
	 
	    public MyFragmentPagerAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
	        super(fragmentManager);
	        this.fragmentsList = fragments;
	    }
	 
	    @Override
	    public int getCount() {
	        return fragmentsList.size();
	    }
	 
	    @Override
	    public Fragment getItem(int arg0) {
	        return fragmentsList.get(arg0);
	    }
	 
	    @Override
	    public int getItemPosition(Object object) {
	        return super.getItemPosition(object);
	    }
	 
	}


	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int pos) {
		if(pos == 0){
			ic_left.setVisibility(View.GONE);
			ic_right.setVisibility(View.GONE);
		}else if(pos == 2){
			ic_left.setVisibility(View.GONE);
			ic_right.setVisibility(View.VISIBLE);
			ic_right.setImageResource(R.drawable.ic_edit);
		}else if(pos == 1){
			ic_left.setVisibility(View.VISIBLE);
			ic_right.setVisibility(View.VISIBLE);
			ic_left.setImageResource(R.drawable.ic_bag);
			ic_right.setImageResource(R.drawable.ic_add);
		}else{
			ic_left.setVisibility(View.GONE);
			ic_right.setVisibility(View.GONE);
		}
	}

	
	public void setViewPagerPosition(int pos){
		viewPager.setCurrentItem(1);
		int id = R.id.layout_mall;
		if(pos == 0){
			id = R.id.layout_mall;
		}else if(pos == 1){
			id = R.id.layout_fitting;
		}else if(pos == 2){
			id = R.id.layout_cool;
		}else if(pos == 3){
			id = R.id.layout_mine;
		}
		View view = new View(this);
		view.setId(id);
		onClick(view);
	}
	
	private long mBackTime = 0;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - mBackTime) < 2000) {
				finish();
			}else{
				mBackTime = System.currentTimeMillis();
				Toast.makeText(getApplicationContext(), "再点一次退出应用", Toast.LENGTH_SHORT).show();
			}
		}
		return true;
	}
}
